<?php
namespace app\wxapp\controller;

use app\model\Record; 
use app\model\Util; 
use app\model\Ad; 
use app\model\Theme; 
use app\model\Sms; 
use app\model\Worker; 
use app\model\Fans; 
use app\model\Userdata; 

class MysteryBoxes extends Base
{
    public function Mystery_boxes()
    {
global $_P, $_G;
$fans = $this->checkuser();
$system=$_G['system'];
$themeid = intval($_P['themeid']);
$theme = Theme::getThemeByIdxcx($themeid);
$config = Theme::getConfig($themeid);
if(empty($theme['id'])){
    wxapp_result(1, '活动入口不正确');
    exit;
}


$op = !empty($_P['op']) ? $_P['op'] : 'display';

//获取定单信息：theMyRecord
if ($config['maxcount']>0) {
    $recordid = intval($_P['recordid']);
}

extract(Record::getTheMyRecord($system,$fans,$recordid,$config,$themeid));
extract(Record::getShowRecord($system,$recordid,$config,$themeid));
if (Record::isFreeJoin($config,$themeid) && ($config['touxiang']==2 || $config['touxiang']==3)) {
    $config['touxiang']=1;
}
if ($op=='menu') {
//    按扭文字处理
    $self = Ad::getSelfInfo($fans, $config, $themeid, $system);

    $rank_show = false;
    if ($config['rank_show'] == 3 && $config['rank_order'] != 0) {
        if ($config['rank_user_type'] == 2) {
            $rank_show = Record::getTuanzhangbyUid($themeid, $_G['member']['uid']);
        } else {
            $rank_show = true;
        }
    }

    $mystery_boxes_yukuan = Fans::getMysteryboxesYukuanStatus($system, $config, $_G['openid'], $themeid);

    if ($config['yukuan'] > 0 && (TIMESTAMP > $config['yukuantime'] || $mystery_boxes_yukuan)) {
        $joinfee = $config['yukuan'];
    } else {
        $joinfee = $config['joinfee'];
    }
    $mystery_menu_new_group = Theme::ButtonTextReplaceXCX($config['mystery_menu_new_group'], $joinfee, $config['primeprice'], $config['groupprice']);
    $mystery_menu_join_group = Theme::ButtonTextReplaceXCX($config['mystery_menu_join_group'], $joinfee, $config['primeprice'], $config['groupprice']);
    $mystery_menu_lump_sump = Theme::ButtonTextReplaceXCX($config['mystery_menu_lump_sump'], $joinfee, $config['primeprice'], $config['groupprice']);
    $mystery_topay_new_user = Theme::ButtonTextReplaceXCX($config['mystery_topay_new_user'], $joinfee, $config['primeprice'], $config['groupprice']);
    $mystery_topay_join_group = Theme::ButtonTextReplaceXCX($config['mystery_topay_join_group'], $joinfee, $config['primeprice'], $config['groupprice']);
    $mystery_topay_new_group = Theme::ButtonTextReplaceXCX($config['mystery_topay_new_group'], $joinfee, $config['primeprice'], $config['groupprice']);

    if (strpos($mystery_menu_lump_sump, "#all")) {
        $mystery_menu_lump_sump = str_replace("#all", "@", $mystery_menu_lump_sump);
        $mystery_menu_lump_sump = explode("@", $mystery_menu_lump_sump);
        $mystery_menu_lump_sump[2] = str_replace(".00", "", $config['mystery_lump_sump']) . '元';
    } else {
        $mystery_menu_lump_sump = array(
            0 => $mystery_menu_lump_sump,
            1 => '',
        );
    }

    $left_up = $system['is_mystery_boxes'] > 0 ? $config['mystery_lump_sump'] : $config['groupprice'];
    $bottombut['left']['up'] = empty($config['mystery_menu_ad1']) ? $left_up : $config['mystery_menu_ad1'];
    $bottombut['left']['up_yuan'] = empty($config['mystery_menu_ad1']) ? 1 : 0;
    if ($config['maxcountall'] <= 10) {
        $ren = array('单', '单', '两', '三', '四', '五', '六', '七', '八', '九');
        $ren = $ren[$config['maxcountall']];
    } else {
        $ren = $config['maxcountall'];
    }
    $config['mystery_menu_ad2'] = str_replace("#ren", $ren, $config['mystery_menu_ad2']);

    if ($config['mystery_lump_sump'] > 0) {
        $mystery_topay_tuan_tips = str_replace("#all", $config['mystery_lump_sump'], $config['mystery_topay_tuan_tips']);
        $mystery_topay_tuan_tips = str_replace(".00", "", $mystery_topay_tuan_tips);
        $mystery_topay_tuan_tips = str_replace("#ren", $ren, $mystery_topay_tuan_tips);
    } else {
        $mystery_topay_tuan_tips = '';
    }
    $bottombut['left']['down'] = $config['mystery_menu_ad2'];


    //0:未报名
    //1：团未满：加入团
    //2：团已满：开新团
    //3:查看自个的团状态2
    //21：团已满：还可加入


    // 20联系客服
    // 21开新团
    // 22一键拼团
    // 23拼团进度
    // 24盲盒进度
    // 25全款支付
    // 26盲盒海报
    // 27返回首页
    // 28拼团海报
    // 29我的盲盒
    // 31重新填写表单：补余款
    // 32直接补余款

    $tuan = array();
    $tuan['status'] = 0;
    $tuan['mystery_topay_tuan_tips'] = $mystery_topay_tuan_tips;
    if ($config['mystery_is_preview'] == 1) {
        $themyrecordid = 1;
        $nowgroupneednum = 0;
    }
    if ($_P['mystery_boxes_example']==1){
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
        if (!empty($myworker)) {
            $themyrecordid = 1;
            $nowgroupneednum = 0;
        }
    }

    if ($config['yukuan'] > 0 && (TIMESTAMP > $config['yukuantime'] || $mystery_boxes_yukuan)) {
        $orderpay = Record::get_orderpay($config, $themeid);
//            $yukuan_order = Record::getRecordYukuanByUid($config,$_G['member']['uid'],$themeid);
//            if ($yukuan_order){
//                $bottombut['left']['txt'][0] = "";
//                $bottombut['left']['down'] = "";
//                if ($config['ishexiao']==1) {
//                    $bottombut['center']['txt'][0] = "查看我的定单";
//                    $bottombut['center']['url'] = 12;
//                }else{
//                    $bottombut['center']['txt'][0] = "查看支付凭证";
//                    $bottombut['center']['url'] = 11;
//                }
//                $bottombut['right']['txt'][0] = '邀请好友参与';
//                $bottombut['right']['url'] = 28;
//            }else{
//            }
        if ($mystery_boxes_yukuan) {
            if ($orderpay > 0) {
                if (empty($config['presale']['menubtn_pay'])) {
                    if (Record::isFreeJoin($config,$themeid)) {
                        $config['presale']['menubtn_pay'] = '立即支付#fee元';
                    } else {
                        $config['presale']['menubtn_pay'] = '立即补余款#fee元';
                    }
                }
                $bottombut['right']['txt'][0] = str_replace("#fee", $orderpay, $config['presale']['menubtn_pay']);
                $bottombut['right']['url'] = $config['presale']['ispresaleform'] == 1 ? 31 : 32;
            }
        }

//            $orderpay = Record::get_orderpay($config,$themeid);
        if ($orderpay > 0 && !empty($themyrecordid)) {
            if ($config['presale']['presalePrice'] > 1) {
                $config['groupprice'] = $config['presale']['groupprice'];
                $formtextcenter = str_replace("#fee", $orderpay, $config['presale']['formtextcenter']);
                $bottombut['left']['txt'] = explode("#br", $formtextcenter);
                $bottombut['left']['url'] = 101;

                $config['groupprice2'] = $config['presale']['groupprice2'];
                $formtextcenter2 = str_replace("#fee", $orderpay, $config['presale']['formtextcenter2']);
                $bottombut['center']['txt'] = explode("#br", $formtextcenter2);
                $bottombut['center']['url'] = 102;

                if ($config['presale']['presalePrice'] > 2) {
                    $config['groupprice3'] = $config['presale']['groupprice3'];
                    $formtextcenter3 = str_replace("#fee", $orderpay, $config['presale']['formtextcenter3']);
                    $bottombut['right']['txt'] = explode("#br", $formtextcenter3);
                    $bottombut['right']['url'] = 103;
                }
            } else {
                if (empty($config['presale']['menubtn_pay'])) {
                    $config['presale']['menubtn_pay'] = '立即补余款￥#fee元';
                }
                $bottombut['right']['txt'][0] = str_replace("#fee", $orderpay, $config['presale']['menubtn_pay']);
                $bottombut['right']['url'] = 10;
                if (!empty($themyrecordid)) {
                    $formtextcenter = str_replace("#fee", $orderpay, $config['presale']['menubtn_pay']);
                } else {
                    $formtextcenter = str_replace("#fee", $orderpay, $config['presale']['menubtn_nopay']);
                }
            }
        }
    }

    if ($orderpay==0 || empty($themyrecordid)) {
        if (empty($themyrecordid) && !$mystery_boxes_yukuan) {
            $bottombut['left']['url'] = 0;
            if (empty($showrecord)) {
                $bottombut['right']['txt'][0] = $mystery_menu_new_group;
                $bottombut['right']['url'] = 21;
                $bottombut['topay'] = $mystery_topay_new_user;
                $tuan['status'] = 0;
                $tuan['time'] = $theme['endtime'];
                $tuan['time_txt'] = 0;
            } else {
                $bottombut['right']['txt'][0] = $mystery_menu_join_group;
                $bottombut['right']['url'] = 22;
                if ($nowgroupneednum > 0) {
                    $tuan['status'] = 1;
                    $bottombut['topay'] = $mystery_topay_join_group;
                    if ($config['isautorefund'] == 0) {
                        $tuan['time'] = $theme['endtime'];
                        $tuan['time_txt'] = 0;
                    } else {
                        $tuan['time'] = $showrecord['addtime'] + $config['groupneedtime'] * 3600;
                        $tuan['time_txt'] = 1;
                    }
                } else {
                    if ($config['tuanmode'] == 1 || $config['tuanmode'] == 3) {
                        $tuan['status'] = 21;
                        $bottombut['topay'] = $mystery_topay_join_group;
                    } elseif ($config['tuanmode'] == 0 || $config['tuanmode'] == 2) {
                        $tuan['status'] = 2;
                        $bottombut['topay'] = $mystery_topay_new_group;
                    }
                }
            }
        } else {
            //若开启补余款
            $orderpay = 0;
            if ($orderpay <= 0 && empty($yukuan_order)) {
                $bottombut['left']['url'] = 23;
                if ($nowgroupneednum > 0 || $config['get_mystery_boxes'] == 2 || $config['get_mystery_boxes'] == 3) {
                    if (Record::isFreeJoin($config,$themeid)) {
                        $bottombut['right']['txt'][0] = '邀请好友参与';
                        $bottombut['right']['url'] = 28;
                    } else {
                        $bottombut['left']['txt'][0] = "";
                        $bottombut['left']['down'] = "";
                        if ($config['maxcountall'] > 1) {
                            $bottombut['center']['txt'][0] = $config['mystery_menu_view_tuan'];
                            $bottombut['center']['url'] = 23;
                        } elseif ($config['ishexiao'] == 1) {
                            $bottombut['center']['txt'][0] = "查看我的定单";
                            $bottombut['center']['url'] = 12;
                        } else {
                            $bottombut['center']['txt'][0] = "查看支付凭证";
                            $bottombut['center']['url'] = 11;
                        }
                        $bottombut['right']['txt'][0] = '邀请好友参与';
                        $bottombut['right']['url'] = 28;
                    }

                    $tuan['status'] = 3;
                    if ($config['isautorefund'] == 0) {
                        $tuan['time'] = $theme['endtime'];
                        $tuan['time_txt'] = 0;
                    } else {
                        $tuan['time'] = $themyrecord['addtime'] + $config['groupneedtime'] * 3600;
                        $tuan['time_txt'] = 1;
                    }
                } else {
                    $tuan['status'] = 4;
                    if ($config['get_mystery_boxes'] != 1) {
                        $bottombut['right']['txt'][0] = '邀请好友参与';
                        $bottombut['right']['url'] = 28;
                    } else {
                        $bottombut['right']['txt'][0] = $config['mystery_menu_view_boxs'];
                        $bottombut['right']['url'] = 24;
                    }

                    $tuan['time'] = $theme['endtime'];
                    //盲盒按扭
                    $isbukuan = db_find('SELECT * FROM ' . tablename(t_record) . " as r
                        WHERE r.themeid ='{$themeid}' and uid = '{$_G['member']['uid']}' and  r.type = 8 and r.status > 0");
                    if (!empty($isbukuan)) {
                        $bottombut2['left']['txt'] = array('已支付', '元', $isbukuan['fee']);
                        $bottombut2['left']['url'] = 23;
                    } else {
                        $bottombut2['left']['txt'] = $mystery_menu_lump_sump;
                        $bottombut2['left']['url'] = 25;
                    }


                    if ($self['is_get_box'] && $self['is_finish_friend'] && $self['is_open_boxs']) {
                        $bottombut2['right']['txt'] = '我的盲盒';
                        $bottombut2['right']['url'] = 29;
                    } else {
                        $bottombut2['right']['txt'] = '盲盒邀请海报';
                        $bottombut2['right']['url'] = 26;
                    }
                }

                if ($rank_show) {
                    $bottombut['left']['url'] = 43;
                    $bottombut['left']['txt'][0] = str_replace('查看', '', $config['rank_view_txt']);
                }
            }
        }
    }

    $tuan['time']= array(date("Y-m-d H:i:s", $tuan['time']));
    $tuan['neednum'] = $nowgroupneednum;
    $tuan['nowrecord']=$nowrecord;
    $tuan['groupneedtime']=$config['isautorefund'];
    //判断盲盒开启状态 引导图 未报名 已报名的判断 未报名只填写信息  已报名 填写信息 拉起支付
    $box_room_img = '';
    $box_to_pay = 0;
    $config_new = array(
        'mystery_boxes_left_color1' => $config['mystery_boxes_left_color1'],
        'mystery_boxes_left_color2' => $config['mystery_boxes_left_color2'],
        'mystery_boxes_right_color1' => $config['mystery_boxes_right_color1'],
        'mystery_boxes_right_color2' => $config['mystery_boxes_right_color2'],
        'mystery_boxes_left_font_color1' => $config['mystery_boxes_left_font_color1'],
        'mystery_boxes_left_font_color2' => $config['mystery_boxes_left_font_color2'],
        'mystery_boxes_right_font_color' => $config['mystery_boxes_right_font_color'],
        'mystery_boxes_customer_font_color' => $config['mystery_boxes_customer_font_color'],
        'mystery_boxes_customer_background_color' => $config['mystery_boxes_customer_background_color'],
        'mystery_boxes_right_font_size' => $config['mystery_boxes_right_font_size'].'rpx',
        'mystery_boxes_right_font_bold' => $config['mystery_boxes_right_font_bold'],
        'mystery_is_lump_sump' => $config['mystery_is_lump_sump'],
        'mystery_menu_show_time' => $config['mystery_menu_show_time'],
        'mystery_join_info' => $config['mystery_join_info'],
    );

    if ($config['get_mystery_boxes']==2 || $config['get_mystery_boxes']==3){
        $config_new['mystery_boxes_room_img'] = ($fans['username'] && $fans['click_open_box'])?'':$config['box_room_img'];
        if ($config['get_mystery_boxes']==3){
            $config_new['mystery_boxes_modal_to_pay'] = 1;
        }

        $config_new['myboxs_btn'] = array(
            'text' => $config['mystery_my_join_text'],
            'share' => $config['mystery_my_share_text'],
            'height' => $config['mystery_my_join_height'],
            'color' => $config['mystery_my_join_color'],
            'font_color' => $config['mystery_my_join_text_color'],
        );
    }
    if ($theme['status']==2 && $config['isshow_end_footermenu']!=1){
        if (empty($themyrecordid)){
            $bottombut['left']['txt'][0] = '';
            $bottombut['left']['url'] = '';
            $bottombut['center']['txt'][0]= '';
            $bottombut['center']['url']= '';
            $bottombut['right']['txt'][0]= '活动已结束';
            $bottombut['right']['url']= 998;
        }
    }
    if ($theme['begintime']>time()){
        $bottombut['left']['txt'][0] = '';
        $bottombut['left']['url'] = '';
        $bottombut['center']['txt'][0]= '';
        $bottombut['center']['url']= '';
        $bottombut['right']['txt'][0]= '活动尚未开始';
        $bottombut['right']['url']= 997;
    }
    $btn = 0;
    if ($bottombut['left']['txt'][0]){
        $btn ++;
    }
    if ($bottombut['center']['txt'][0]){
        $btn ++;
    }
    if ($bottombut['right']['txt'][0]){
        $btn ++;
    }
    if ($bottombut['left']['down']){
        if ($btn==2){
            $btn_width = 260;
        }else{
            $btn_width = 420;
        }
    }elseif ($btn>0){
        $btn_width = intval(660/$btn);
    }
    if ($config_new['mystery_boxes_right_font_size']==45){
        if ($btn==3){
            $config_new['mystery_boxes_right_font_size']='33rpx';
        }elseif ($btn==2){
            $config_new['mystery_boxes_right_font_size']='38rpx';
        }
    }
    $bottombut['btn_width']= $btn_width;

    $result = array(
        'bottombut'=>$bottombut,
        'bottombut2'=>$bottombut2,
        'tuan'=>$tuan,
        'config'=>$config_new,
    );

    wxapp_result(0, '',$result);
}
else if ($op=='display'){
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if ($_P['example']==1){
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
        if (!empty($myworker)){
            $gift = Ad::getAddressGift($themeid,'show');
            $xuni = Fans::getXuniFans($config,1,1,1);

            $self = array(
                'avatar'=>$xuni[0]['avatar'],
                'username'=>$xuni[0]['username'],
                'is_finish_friend'=> true,
                'is_get_box'=> true,
                'is_open_boxs'=> true,
                'mystery_process_show'=> 0,
                'mystery_process_show_small_img'=> "https://css2020.yixuan.net/2020css/mystery_boxes/small_img.png",
                'need_friend_num'=>null,
                'gift'=>$gift[rand(0,count($gift)-1)],
                'is_zhifu'=>true,
                'is_pingtuan'=>true,
                'mystery_gift_select'=>0,
                'is_select_gift'=>true,
            );

            if ($config['mystery_gift_select']!=1){
                $open_num = Ad::get_boxes_max_num($config);
            }else{
                $open_num = 1;
            }
            if ($open_num==1){
                $self['gifts'][0]=$gift[rand(0,count($gift)-1)];
            }else{
                $gifts_key =array_rand ($gift, $open_num);
                foreach ($gifts_key as $item){
                    $self['gifts'][]=$gift[$item];
                }
            }
        }
    }
    //获取当前用户
    if ($self['is_get_box']){
        $mystery_page_lump_sump_text = Theme::ButtonTextReplaceXCX($config['mystery_page_lump_sump_text'],$config['joinfee'],$config['primeprice'],$config['groupprice']);
        $mystery_page_lump_sump_text = str_replace("#all",$config['mystery_lump_sump'].'元',$mystery_page_lump_sump_text);
        $mystery_page_lump_sump_text=str_replace(".00","",$mystery_page_lump_sump_text);

        $isbukuan = db_find ( 'SELECT * FROM ' . tablename ( t_record ) . " as r
                WHERE r.themeid ='{$themeid}' and uid = '{$_G['member']['uid']}' and  r.type = 8 and r.status > 0" );

        if (!empty($isbukuan)){
            $bottombut2_url = $config['ishexiao']==1?11:12;
            $mystery_page_lump_sump_text = str_replace("#all",$isbukuan['fee'].'元',$config['mystery_page_lump_sump_success_text']);
        }else{
            $bottombut2_url = 25;
        }
        if ($config['get_mystery_boxes']==1){
            $page_button=array(
                'lump_sump'=>array(
                    'text'=>$mystery_page_lump_sump_text,
                    'height'=>$config['mystery_page_lump_sump_height'],
                    'color'=>$config['mystery_page_lump_sump_color'],
                    'font_color'=>$config['mystery_page_lump_sump_font_color'],
                    'url' => $bottombut2_url,
                ),
                'friend'=>array(
                    'text'=>$config['mystery_page_friend_text'],
                    'success'=>$config['mystery_page_friend_success_text'],
                    'height'=>$config['mystery_page_friend_height'],
                    'color'=>$config['mystery_page_friend_color'],
                    'font_color'=>$config['mystery_page_friend_font_color'],
                ),
                'avatar'=>array(
                    'size'=>$config['mystery_page_avatar_size'],
                    'height'=>$config['mystery_page_avatar_height'],
                    'font_color'=>$config['mystery_page_avatar_color'],
                ),
                'myboxs_btn'=>array(
                    'text'=>$config['mystery_my_join_text'],
                    'share'=>$config['mystery_my_share_text'],
                    'height'=>$config['mystery_my_join_height'],
                    'color'=>$config['mystery_my_join_color'],
                    'font_color'=>$config['mystery_my_join_text_color'],
                ),
            );
            if ($config['mystery_is_lump_sump']==0){
                $page_button['lump_sump']['height']=-300;
            }
        }

        if (!empty($_G['member']['uid'])){
            $params[':fromuid'] = $_G['member']['uid'];
            $params[':themeid'] = $themeid;
            $sql = "SELECT s.nickname,s.avatar,s.uid,s.username,s.mobile,d.uid,d.fromuid,d.addtime 
                FROM " . tablename ( t_friend ) . " as d 
                left join " . tablename ( t_fans ) . " as s on s.uid = d.uid and s.themeid=d.themeid 
                WHERE d.themeid = :themeid and d.fromuid = :fromuid order by d.addtime";
            $friend_detail = db_select ( $sql,$params);
        }

        if (!empty($friend_detail)){
            foreach ($friend_detail as $index =>$item){
                $friend_detail[$index]['nickname']=mb_substr($item['nickname'],0,4,'utf-8');
            }
        }
    }


    $config_new=array(
        'mystery_friend_need'=>$self['need_friend_num']+count($friend_detail),
        'mystery_boxes_content_top_xcx' => Theme::format_jpg($config['mystery_boxes_content_top_xcx'],$fans,$theme,$system),
        'mystery_boxes_content_bottom_xcx' => Theme::format_jpg($config['mystery_boxes_content_bottom_xcx'],$fans,$theme,$system),
        'time'=>array(date("Y-m-d H:i:s", $theme['endtime'])),
        'but_friend_help'=>'帮TA拆盲盒',
        'but_friend_help_success'=>'已帮TA拆盲盒',
        'but_friend_help_success_no_reg'=>'已帮TA拆盲盒,我也参加',
        'but_self_no_reg'=>'点击我也参加活动',
        'but_self_reg'=>'查看自己的进度',
        'but_self_share'=>'点击邀请好友拆盲盒',
        'but_self_open_boxs'=>'立即拆盲盒',
        'friend_help_success'=>'感谢你帮我拆',
        'friend_help_success2'=>'幸运盲盒！',
        'friend_finish'=>'通知TA拆盲盒',
        'but_self_success'=>'联系我们【领取礼品】',
        'mystery_show_time'=>$config['mystery_show_time'],
        'mystery_friend_mode4'=>$config['mystery_friend_mode4'],
        'mystery_friend_mode5'=>$config['mystery_friend_mode5'],
        'mystery_gift_autoplay' => $config['mystery_gift_autoplay'],
        'get_mystery_boxes' => $config['get_mystery_boxes'],
        'mystery_gift_select' => $config['mystery_gift_select'],
        'mystery_help_img' => $config['mystery_help_img'],
        'mystery_help_img_width' => $config['mystery_help_img_width'],
        'mystery_tips_top' => $config['mystery_tips_top'],
        'mystery_tips_button' => $config['mystery_tips_button'],
        'mystery_tips_txt' => $config['mystery_tips_txt'],
    );

    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    }
    if ($config['mystery_is_preview']==1){
        $nowrecord = array(
            "tips"=>"此团还差1人 立即加入",
            "zhang"=>array(
                "avatar"=>$fans['avatar'],
                "username"=>mb_substr($fans['nickname'],0,4,'utf-8'),
            ),
            "detail"=>array(
                0=>array(
                    "img"=>"https://css2020.yixuan.net/2020css/fcpdd/images/ask.png",
                    "username"=>"",
                ),
            )
        );
    }
    //判断A访问B
    if ($friend && $friend['is_friend_help']){
        //a报名
        if ($fans['click_open_box']){
            $friend = [];
        }
    }

    if ($config['get_mystery_boxes']==2){
        $config_new['friend_status_tips'] = $friend['username'];
    }elseif ($config['get_mystery_boxes']==1){
        $config_new['friend_status_tips'] = '拼团成功';
    }elseif ($config['get_mystery_boxes']==3){
        $config_new['friend_status_tips'] = '抢购成功';
    }


    $result = array(
        'config'=>$config_new,
        'page_button'=>$page_button,
        'nowrecord'=>$nowrecord,
        'friend_detail'=>$friend_detail,
        'friend'=>$friend,
        'self'=>$self,
        'gift'=>Ad::getAddressGift($themeid,'show'),
    );
    wxapp_result(0, '',$result);
}
else if ($op=='open_boxs'){
    if ($config['mystery_boxes_endtime_set']==1 && $config['mystery_boxes_endtime']<TIMESTAMP || $config['mystery_boxes_endtime_set']==0 && $theme['status'] == 2){
        wxapp_result(1, '时间过期');
    }
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if (!$self['is_get_box']){
        wxapp_result(1, '未获得盲盒');
    }
    if (!$self['is_finish_friend']){
        wxapp_result(1, '邀请好友助力未完成');
    }
    if ($self['is_open_boxs']){
        wxapp_result(1, '盲盒已开启');
    }else{
        //判断用的哪个条件拆的盲盒
        $fans = db_get(t_fans,['id'=>$fans['id'],'themeid'=>$themeid]);
        $fans['other_info'] = unserialize($fans['other_info']);
        if ($fans['other_info']['mode5']==1){
            $fans['other_info']['mode5']=2;
            $op_tips = '拼团成功';
        }elseif ($fans['other_info']['mode4']==1){
            $fans['other_info']['mode4']=2;
            $op_tips = '支付成功';
        }elseif ($fans['other_info']['need0']==1){
            $fans['other_info']['need0']=2;
            $op_tips = '助力达到' . $config['mystery_friend_need']. '人';
        }elseif ($fans['other_info']['need1']==1){
            $fans['other_info']['need1']=2;
            $op_tips = '助力达到' . $config['mystery_friend_need']+$config['mystery_friend_need2']. '人';
        }elseif ($fans['other_info']['need2']==1){
            $fans['other_info']['need2']=2;
            $op_tips = '助力达到' . $config['mystery_friend_need']+$config['mystery_friend_need2']+$config['mystery_friend_need3']. '人';
        }
        if (empty($op_tips)){
            wxapp_result(1, '点的太快了');
        }
        db_update(t_fans,array('other_info'=>serialize($fans['other_info'])),array('id'=>$fans['id']));

        $result = Ad::getAddressGift($themeid, 'get');
        if (empty($result)){
            wxapp_result(1, '盲盒礼品未配置');
        }
        if ($config['get_mystery_boxes']==2){
            $formuid_record = Record::getRecordByUidNopay($_G['member']['uid'],$themeid);
        }else{
            $formuid_record = Record::getRecordByUid($_G['member']['uid'],$themeid);
        }

        $beizhu = date('Y-m-d H:i:s').' '.$op_tips.' 拆开宝盒 获得【' . $result['name'] . '】 操作:' . $_G['clientip'] ;

        Userdata::addLog($themeid, $_G['member']['uid'], 29,$beizhu);
        if (!empty($formuid_record['address'])){
            $giftname = explode('|', $formuid_record['address']);
            if ($config['mystery_gift_get']==0){
                $giftname = array_flip($giftname);
                $giftname = array_flip($giftname);
            }
            $giftname = implode('|',$giftname) . "|";
        }else{
            $giftname='';
        }
        $data=array(
            'address' => $giftname . $result['name'],
            'beizhu' => $formuid_record['beizhu'].$beizhu. '<br>',
        );

        db_update(t_record, $data, array('id' => $formuid_record['id']));

        $self['gift'] = Ad::getFansBoxGift($config, $result['name']);
        $self['is_open_boxs'] = true;
    }

    $result = array(
        'self'=>$self,
    );
    wxapp_result(0, '',$result);
}
else if ($op=='select_gifts'){
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    $giftName = $_P['giftName'];
    if (empty($giftName)){
        wxapp_result(1, '请选择礼品');
    }
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if (!$self['is_get_box']){
        wxapp_result(1, '未达到拆盲盒条件');
    }
    if (!$self['is_finish_friend']){
        wxapp_result(1, '邀请好友助力未完成');
    }
    if (count($self['gifts'])<2){
        wxapp_result(1, '盲盒礼品参数错误');
    }
    if ($self['is_select_gift']){
        wxapp_result(1, '盲盒礼品已选择');
    }else{
        foreach ($self['gifts'] as $index => $row) {
            if ($giftName==$row['name']){
                $select_gift = $row['name'];
            }
        }
        if (empty($select_gift)){
            wxapp_result(1, '礼品参数错误：未找到所选礼品');
        }else{
            if ($config['get_mystery_boxes']==2){
                $formuid_record = Record::getRecordByUidNopay($_G['member']['uid'],$themeid);
            }else{
                $formuid_record = Record::getRecordByUid($_G['member']['uid'],$themeid);
            }
            $beizhu = date('Y-m-d H:i:s').' 选择礼品【' . $select_gift . '】 操作:' . $_G['clientip'] ;
            Userdata::addLog($themeid, $_G['member']['uid'], 30,$beizhu);
            $data=array(
                'address' => $formuid_record['address'] ."|选中".$select_gift,
                'beizhu' => $formuid_record['beizhu'].$beizhu. '<br>',
            );
            db_update(t_record, $data, array('id' => $formuid_record['id']));
        }
        $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    }
    $result = array(
        'self'=>$self,
    );
    wxapp_result(0, '',$result);
}
else if ($op=='friend_help'){
    if ($config['mystery_boxes_endtime_set']==1 && $config['mystery_boxes_endtime']<TIMESTAMP || $config['mystery_boxes_endtime_set']==0 && $theme['status'] == 2){
        wxapp_result(1, '时间过期');
    }
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    }else{
        wxapp_result(1, '助力失败：不可给自己助力');
    }
    if (!$friend['is_get_box']){
        wxapp_result(1, '助力失败：TA未获得盲盒');
    }
    if ($friend['is_finish_friend']){
        wxapp_result(1, '助力失败：TA的助力已完成');
    }
    $is_friend_help = Record::isFriendHelp($_G['member']['uid'],$_P['fromuid'],$themeid);
    if ($friend['is_friend_help'] || $is_friend_help){
        wxapp_result(1, '助力失败：已帮TA助力过');
    }
    db_run('set AUTOCOMMIT=0');//关闭自动提交，自此句执行以后，每个SQL语句或者语句块所在的事务都需要显示"commit"才能提交事务
    db_run('START TRANSACTION');//启动一个新事务
    $data = array(
        'themeid'   => $themeid,
        'uniacid'   => $_G['mpid'],
        'uid'       => $_G['member']['uid'],
        'openid'    => $_G['openid'],
        'status'    => 1,
        'addtime'   => TIMESTAMP,
        'fromuid'  => $_P['fromuid'],
    );

    if (!empty($_P['onlyinfo'])){
        if (isset($_P['username']) && $_P['username'] && isset($_P['mobile']) && $_P['mobile']){
            $pm = array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']);
            db_query("UPDATE ".tablename(t_fans)." SET username = '{$_P['username']}',mobile = '{$_P['mobile']}' WHERE themeid =:themeid and uid = :uid",$pm);
            $fans['username'] = $_P['username'];
            $fans['mobile'] = $_P['mobile'];
        }
    }
    $r1 = $insertid = db_insert(t_friend, $data);


    $pm = array(":themeid"=>$themeid,":uid"=>$_P['fromuid']);
    $r2 = db_query("UPDATE ".tablename(t_fans)." SET friendnum = friendnum +1 WHERE themeid =:themeid and uid = :uid",$pm);

    if (!$r1 || !$r2) {
        db_run('ROLLBACK');//事务回滚
        wxapp_result(1, '助力失败：未知错误');
    }else{
        Userdata::addLog($themeid, $_G['member']['uid'], 31,'',0,$_P['fromuid']);
        db_run('COMMIT');//事务提交
    }

    $friend_new = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    if ($friend_new['is_finish_friend']==true && $friend_new['is_finish_friend']!=$friend['is_finish_friend']){
        Util::send_mb_help_success($system,$config,$theme,$_P['fromuid']);
        $fans['success_mode']=1;
        Sms::CheckAndSendSms('mystery_boxes_new',$system,$config,$themeid, $fans,$data);
    }else{
        Sms::CheckAndSendSms('friend_help',$system,$config,$themeid, $fans,$data);
    }
    $result = array(
        'friend'=>$friend_new,
    );
    $result['ad_info'] = Ad::get_ad_info($config,$fans,$themeid,'','wxapp','friendhelp');
    wxapp_result(0, '',$result);
}

}
}